package com.lhkj.ct.meta.modules.psychology.service.impl;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.core.metadata.SqlOrder;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.base.utils.file.excel.ExcelUtils;
import com.lhkj.ct.meta.modules.gauge.mapper.ScaleMapper;
import com.lhkj.ct.meta.modules.psychology.mapper.ScalePsyImportMapper;
import com.lhkj.ct.meta.modules.psychology.model.bo.PsyExportParam;
import com.lhkj.ct.meta.modules.psychology.model.entity.TblScalePsyImport;
import com.lhkj.ct.meta.modules.psychology.service.PsyExportProcess;
import com.lhkj.ct.meta.modules.psychology.service.PsyImportProcess;
import com.lhkj.ct.meta.modules.psychology.service.ScalePsyImportService;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-10-23
 */
@Service
public class ScalePsyImportServiceImpl extends IServiceImpl<ScalePsyImportMapper, TblScalePsyImport> implements ScalePsyImportService {

    private final Logger logger = LoggerFactory.getLogger(ScalePsyImportServiceImpl.class);

    @Resource
    private PsyExportProcess exportService;

    @Resource
    private PsyImportProcess importProcess;

    @Resource
    private ScaleMapper scaleMapper;

    @Override
    public IPage<TblScalePsyImport> selectImportPage(TblScalePsyImport psyImport, PageQuery pageQuery) {
        return getBaseMapper().selectImportPage(generatePage(pageQuery, SqlOrder.desc(TblScalePsyImport::getCreateTime)), psyImport, pageQuery);
    }

    @Override
    public void export(List<String> ids) {
        if (ids.size() == 1) {
            PsyExportParam exportParam = scaleMapper.getExportParamByScaleId(ids.get(0));
            Validate.notNull(exportParam, "量表不存在或已删除！");
            Workbook workbook = exportService.generateExcel(exportParam);
            ExcelUtils.export(workbook, exportParam.getTitle());
        } else {
            ConcurrentHashMap<String, Workbook> workbooks = new ConcurrentHashMap<>();
            for (String id : ids) {
                try {
                    final PsyExportParam exportParam = scaleMapper.getExportParamByScaleId(id);
                    if (null == exportParam) {
                        continue;
                    }
                    CompletableFuture.runAsync(() -> {
                        workbooks.put(exportParam.getTitle(), exportService.generateExcel(exportParam));
                    }).get();
                } catch (InterruptedException | ExecutionException e) {
                    logger.error("测验导出，量表转excel文件异常：" + ExceptionUtil.getMessage(e));
                }
            }
            ExcelUtils.export(workbooks, "测验导出.zip");
        }
    }

    @Override
//    @Async("scheduledExecutorService")
    public void importPsy(MultipartFile file) {
        importProcess.importProcess(file);
    }
}
